import { checkHttpStatus, parseJSON } from '../utils/index';
import { SHOW_ACCOUNT_STATUS, SHOW_ACCOUNT_PIC} from '../contants/user'

export function showAccountStatus(status, data) {
  return {
    type: SHOW_ACCOUNT_STATUS,
    payload: {
      status: status,
      data: data
    }
  }
}
export function showAccountIcon(status, data) {
  return {
    type: SHOW_ACCOUNT_PIC,
    payload: {
      status: status,
      data: data
    }
  }
}

export function showAccount() {
  var userId = localStorage.getItem('session');
  return dispatch => {
    return fetch('http://fengke.net:8081/user/account_show', {
        method: 'POST',
        headers: {
          'Accept': 'application/json',
          'Content-Type': 'application/json'
        },
        body: JSON.stringify({
          userId: userId
        })
      })
      .then(checkHttpStatus)
      .then(parseJSON)
      .then(
        response => {
          if (response.status == 1) {
            dispatch(showAccountStatus('success', response.data));
          } else {
            dispatch(showAccountStatus('error'));
            console.log(response.errmsg);
          }
        },
        error => {
          console.log('show account fails:' + error);
        })
  }
}

export function showAccountPic() {
  var userId = localStorage.getItem('session');
  return dispatch => {
    return fetch('http://fengke.net:8081/user/showpic', {
        method: 'POST',
        headers: {
          'Accept': 'application/json',
          'Content-Type': 'application/json'
        },
        body: JSON.stringify({
          userId: userId
        })
      })
      .then(checkHttpStatus)
      .then(parseJSON)
      .then(
        response => {
          if (response.status == 1) {
            dispatch(showAccountIcon('success', response.userPic));
          } else {
            dispatch(showAccountIcon('error','gss0.baidu.com/-Po3dSag_xI4khGko9WTAnF6hhy/zhidao/pic/item/bba1cd11728b471092c751b3c0cec3fdfd0323e0.jpg'));
            console.log(response.errmsg);
          }
        },
        error => {
          console.log('show account pic fails:' + error);
        })
  }
}
